package com.xcity.game.shop;

import com.alibaba.fastjson.JSONObject;
import com.xcity.db.entity.Player;
import com.xcity.game.common.PlayerPool;
import com.xcity.pomelo.response.ShopV2Response.ShopItemV2;

import naga.x.game.time.TimeUpdater;

public class ShopUtils {

	public static int getBoughtNumToday(Player player, ShopItemTemplate item) {
		int lastDay = player.getPool().getIntValue(PlayerPool.PROPERTY_LAST_BOUGHT_SHOP_ITEM_DAY);
		if (lastDay != TimeUpdater.getInstance().today()) {
			return 0;
		}
		JSONObject map = player.getPool().getJSONObject(PlayerPool.PROPERTY_BOUGHT_SHOP_ITEM_RECORD);
		if (map == null) {
			return 0;
		}
		return map.getIntValue(item.getUniqueId());
	}
	
	public static void addBoughtNum(Player player, ShopItemTemplate item, int count) {
		int lastDay = player.getPool().getIntValue(PlayerPool.PROPERTY_LAST_BOUGHT_SHOP_ITEM_DAY);
		if (lastDay != TimeUpdater.getInstance().today()) {
			player.poolSet(PlayerPool.PROPERTY_LAST_BOUGHT_SHOP_ITEM_DAY, TimeUpdater.getInstance().today());
			player.poolRemove(PlayerPool.PROPERTY_BOUGHT_SHOP_ITEM_RECORD);
		}
		JSONObject map = player.getPool().getJSONObject(PlayerPool.PROPERTY_BOUGHT_SHOP_ITEM_RECORD);
		if (map == null) {
			map = new JSONObject();
			player.poolSet(PlayerPool.PROPERTY_BOUGHT_SHOP_ITEM_RECORD, map);
		}
		count += map.getIntValue(item.getUniqueId());
		map.put(item.getUniqueId(), count);
	}
	
	public static int getAvailableCount(Player player, ShopItemTemplate item) {
		int availableCount = ShopItemV2.AVAILABLE_ENOUGH;
		if (item.getNumLimit() > 0) {
			availableCount = item.getNumLimit() - getBoughtNumToday(player, item);
			if (availableCount < 0) {
				availableCount = 0;
			}
		}
		return availableCount;
	}
	
}
